<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="优惠券id" prop="couponId">
      <el-input v-model="dataForm.couponId" placeholder="优惠券id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="门店id" prop="storeId">
      <el-input v-model="dataForm.storeId" placeholder="门店id"></el-input>
    </el-form-item>
    <el-form-item label="活动id" prop="activityId">
      <el-input v-model="dataForm.activityId" placeholder="活动id"></el-input>
    </el-form-item>
    <el-form-item label="优惠券名称" prop="couponName">
      <el-input v-model="dataForm.couponName" placeholder="优惠券名称"></el-input>
    </el-form-item>
    <el-form-item label="用户名称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="门店名称" prop="storeName">
      <el-input v-model="dataForm.storeName" placeholder="门店名称"></el-input>
    </el-form-item>
    <el-form-item label="活动名称" prop="activityName">
      <el-input v-model="dataForm.activityName" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="代金券code" prop="couponCode">
      <el-input v-model="dataForm.couponCode" placeholder="代金券code"></el-input>
    </el-form-item>
    <el-form-item label="使用时间" prop="useDate">
      <el-input v-model="dataForm.useDate" placeholder="使用时间"></el-input>
    </el-form-item>
    <el-form-item label="领取时间" prop="takeDate">
      <el-input v-model="dataForm.takeDate" placeholder="领取时间"></el-input>
    </el-form-item>
    <el-form-item label="0 未使用 1已使用" prop="status">
      <el-input v-model="dataForm.status" placeholder="0 未使用 1已使用"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          couponId: '',
          userId: '',
          storeId: '',
          activityId: '',
          couponName: '',
          userName: '',
          storeName: '',
          activityName: '',
          couponCode: '',
          useDate: '',
          takeDate: '',
          status: '',
          createTime: ''
        },
        dataRule: {
          couponId: [
            { required: true, message: '优惠券id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          storeId: [
            { required: true, message: '门店id不能为空', trigger: 'blur' }
          ],
          activityId: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          couponName: [
            { required: true, message: '优惠券名称不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],
          storeName: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          activityName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          couponCode: [
            { required: true, message: '代金券code不能为空', trigger: 'blur' }
          ],
          useDate: [
            { required: true, message: '使用时间不能为空', trigger: 'blur' }
          ],
          takeDate: [
            { required: true, message: '领取时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '0 未使用 1已使用不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/activity/couponrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.couponId = data.couponRecord.couponId
                this.dataForm.userId = data.couponRecord.userId
                this.dataForm.storeId = data.couponRecord.storeId
                this.dataForm.activityId = data.couponRecord.activityId
                this.dataForm.couponName = data.couponRecord.couponName
                this.dataForm.userName = data.couponRecord.userName
                this.dataForm.storeName = data.couponRecord.storeName
                this.dataForm.activityName = data.couponRecord.activityName
                this.dataForm.couponCode = data.couponRecord.couponCode
                this.dataForm.useDate = data.couponRecord.useDate
                this.dataForm.takeDate = data.couponRecord.takeDate
                this.dataForm.status = data.couponRecord.status
                this.dataForm.createTime = data.couponRecord.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/couponrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'couponId': this.dataForm.couponId,
                'userId': this.dataForm.userId,
                'storeId': this.dataForm.storeId,
                'activityId': this.dataForm.activityId,
                'couponName': this.dataForm.couponName,
                'userName': this.dataForm.userName,
                'storeName': this.dataForm.storeName,
                'activityName': this.dataForm.activityName,
                'couponCode': this.dataForm.couponCode,
                'useDate': this.dataForm.useDate,
                'takeDate': this.dataForm.takeDate,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
